<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/public/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>配置管理</title>
<%@ include file="/WEB-INF/page/public/contentmeta.jsp"%>
<link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/global.css" media="all" />
<link rel="stylesheet" href="/css/tab.css" media="all" />
</head>
<body>

<div class="admin-tab admin-user">
    <div class="tab-condition">
        <form class="clefix"  action="/systemer/configurelistc${cid}_r${pid}" id="userlistform"> <!-- layui-form -->
            <div class="layui-form-item">
            	<input name="skipToPage" id="skipToPage"  type="hidden" value="${pager.pageNo}">
				<input type="hidden" id="pageNo" name="pageNo" value="${pager.pageNo}">
				<input type="hidden" id="totalPage" name="totalPage" value="${pager.totalPages}">
            </div>
        </form>
        <div class="TM-tab-info">
        	
        	<a class="layui-btn layui-btn-primary layui-btn-small addconfig" data="${cid}">
        		<c:if test="${cid == 0}">
        			+添加配置项
        		</c:if>
        		<c:if test="${cid != 0}">
        			+添加配置项详情
        		</c:if>
        	</a>
        	<a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary layui-btn-small">
        		<i class="layui-icon">&#xe603;</i>返回
        	</a>
        </div>
    </div>
    <div class="layui-field-box">
    	<c:if test="${cid == 0}">
	    	<table class="site-table table-hover TM-user">
	            <thead>
	            <tr>
	                <th style="width:20%;">配置项</th>
	                <th style="width:10%;">配置项CODE</th>
	                 <th style="width:10%;">操作</th>
	            </tr>
	            </thead>
	            <tbody>
	            	<c:forEach var="conlist" items="${pager.resultList}">
	            	 <tr>
		                <td>
		                	<c:if test="${pid == null || pid == ''}">
		                		<a href="/systemer/configurelistc${conlist.id}_p1">${conlist.name}</a>
		                	</c:if>
		                	<c:if test="${pid != null && pid != ''}">
		                		<a href="/systemer/configurelistc${conlist.id}_r${pid}_p1">${conlist.name}</a>
		                	</c:if>
		                </td>
		                <td>${conlist.code}</td>
		                <td>
		                	<button class="layui-btn layui-btn-primary layui-btn-small update" ciddata="${cid}" iddata="${conlist.id}"  namedata="${conlist.name}" codedata="${conlist.code}"><i class="layui-icon ">&#xe642;</i>修改</button>
	                		<button class="layui-btn layui-btn-primary layui-btn-small delete" typedata="1" ciddata="${conlist.id}"><i class="layui-icon ">&#xe640;</i>删除</button>
		                </td>
		            </tr>
				</c:forEach>
	            </tbody>
	        </table>
    	</c:if>
    	<c:if test="${cid != 0}">
	    	<table class="site-table table-hover TM-user">
	            <thead>
	            <tr>
	                <th style="width:20%;">名称</th>
	                <th style="width:10%;">配置表id</th>
	                <th style="width:10%;">父id</th>
	                <th style="width:10%;">值</th>
	                <th style="width:10%;">排序号</th>
	                <th style="width:10%;">操作</th>
	            </tr>
	            </thead>
	            <tbody>
	            	<c:forEach var="conitemlist" items="${pager.resultList}">
	            	 <tr>
		                <td><a href="/systemer/configurelistc${conitemlist.cid}_r${conitemlist.id}_p1">${conitemlist.name}</a></td>
		                <td>${conitemlist.cid}</td>
		                <td>${conitemlist.pid}</td>
		                <td>${conitemlist.value}</td>
		                <td>${conitemlist.ordernumber}</td>
		                <td>
	                		<button class="layui-btn layui-btn-primary layui-btn-small update" ciddata="${cid}" iddata="${conitemlist.id}" namedata="${conitemlist.name}"
	                		 valuedata="${conitemlist.value}" orderdata="${conitemlist.ordernumber}" id="${conitemlist.id}"><i class="layui-icon ">&#xe642;</i>修改</button>
	                		<button class="layui-btn layui-btn-primary layui-btn-small delete" typedata="2" ciddata="${conitemlist.cid}" iddata="${conitemlist.id}"><i class="layui-icon ">&#xe640;</i>删除</button>
	                	</td>
		            </tr>
				</c:forEach>
	            </tbody>
	        </table>
    	</c:if>
        <input type="hidden" class="cid" id="cid" value="${cid}">
    	<input type="hidden" class="pid" id="pid" value="${pid}">
        <div class="admin-table-page">
            <div id="page" class="page"></div>
        </div>	
    </div>
</div>

<div class="layui-form-item" id="validatebound-formDiv1" style="width:90%;margin-top:20px;display:none">
	<form  class="layui-form clefix" action="" id="validatebound-form1" method="post">
		<div class="layui-form-item">
		    <label class="layui-form-label">名称</label>
		    <div class="layui-input-block">
		    	<input type="text" class="layui-input" id="configname">
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">CODE</label>
		    <div class="layui-input-block">
		    	<input type="text" class="layui-input" id="code">
		    </div>
		</div>
		<div class="layui-form-item">
    		<div class="layui-input-block">
    			<input type="hidden" class="ctype" id="ctype">
    			<input type="hidden" class="conid" id="conid">
     			<button class="layui-btn" lay-submit  lay-filter="configsubmit">保存</button>
	    	</div>
		</div>  
	</form>
</div>

<div class="layui-form-item" id="validatebound-formDiv2" style="width:90%;margin-top:20px;display:none">
	<form  class="layui-form clefix" action="" id="validatebound-form2" method="post">
		<div class="layui-form-item">
		    <label class="layui-form-label">名称</label>
		    <div class="layui-input-block">
		    	<input type="text" class="layui-input" id="configitemname">
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">值</label>
		    <div class="layui-input-block">
		    	<input type="text" class="layui-input" id="value">
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">排序号</label>
		    <div class="layui-input-block">
		    	<input type="text" class="layui-input" id="ordernumber">
		    </div>
		</div>
		<div class="layui-form-item">
    		<div class="layui-input-block">
    			<input type="hidden" class="cid" id="cid" value="${cid}">
    			<input type="hidden" class="pid" id="pid" value="${pid}">
    			<input type="hidden" class="citype" id="citype">
    			<input type="hidden" class="coniid" id="coniid">
     			<button class="layui-btn" lay-submit  lay-filter="configitemsubmit">保存</button>
	    	</div>
		</div>  
	</form>
</div>

<script>

	layui.use(['icheck','element','laypage','form'], function() {
		
		var $ = layui.jquery,
        laypage = layui.laypage,
        element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块
 		var form = layui.form();
        
 		//添加部门
 		$(".addconfig").on("click",function(){
 			var cid = $(this).attr("data");
 			var pid = $("#pid").val();
 			if (cid == 0) {
 				$("#ctype").val(0);
 				layer.open({
 					title: "添加配置",
 					maxmin: true,
 					type: 1,
 					content: $('#validatebound-formDiv1'),
 					area: ['500px', '400px']
 				});
 			} else {
 				$("#citype").val(0);
 				$.ajax({
 					type:"post",
 					data:{cid:cid,pid:pid},
 					url:"/systerm/getmaxordernumber",
 					dataType:"json",
 					success:function(data){
 						if (data != null) {
 							$("#ordernumber").val(data+1);
 						} else {
 							$("#ordernumber").val(data);
 						}
  					}
 				});
 				layer.open({
 					title: "添加配置详情",
 					maxmin: true,
 					type: 1,
 					content: $('#validatebound-formDiv2'),
 					area: ['500px', '400px']
 				});
 			}
 		});
 		
 		//修改部门
 		$(".update").on("click",function(){
 			var cid = $(this).attr("ciddata");
 			var id = $(this).attr("iddata");
 			if (cid == 0) {
 				//修改配置项
 				$("#configname").val($(this).attr("namedata"));
 				$("#code").val($(this).attr("codedata"));
 				$("#conid").val(id);
 				$("#ctype").val(1);
 				layer.open({
 					title: "添加配置",
 					maxmin: true,
 					type: 1,
 					content: $('#validatebound-formDiv1'),
 					area: ['500px', '400px']
 				});
 			} else {
 				$("#configitemname").val($(this).attr("namedata"));
 				$("#value").val($(this).attr("valuedata"));
 				$("#ordernumber").val($(this).attr("orderdata"));
 				$("#citype").val(1);
 				$("#coniid").val(id);
 				layer.open({
 					title: "添加配置详情",
 					maxmin: true,
 					type: 1,
 					content: $('#validatebound-formDiv2'),
 					area: ['500px', '400px']
 				});
 			}
 		});
        
    	var pageNo = $("#pageNo").val();
    	var totalPage = $("#totalPage").val(); 
        //page
        laypage({
            cont: 'page',
            curr: pageNo//'${pager.pageNo}'
            ,pages: totalPage//'${pager.totalPages}' //总页数
            ,
            groups: 5 //连续显示分页数
            ,
            jump: function(obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                if(!first) {
                	$("#skipToPage").val(curr);
                	$("#userlistform").submit();
                }
            }
        });
        
        
        //删除操作
        $(".delete").on("click",function(){
        	
        	var type = $(this).attr("typedata");
        	var cid = $(this).attr("ciddata");
        	var id = 0;
        	if (type == 1) { 
        		//配置表删除
        		layer.confirm('确定删除该配置吗？', {
        			btn: ['确定','取消']
        		}, function(){
        			layer.confirm('该配置删除后，其所有子配置将被删除，且不可恢复，确定删除？',{
        				btn: ['确定','取消']
        			},	function(){
        				delectconfig(type,cid,id);
        			}, function(){
        				layer.msg("取消删除",{icon: 1,time:700});
        			});
        		}, function(){
        			layer.msg("取消删除",{icon: 1,time:700});
        		});
        	} else { 
        		//配置表详情表删除
        		id = $(this).attr("iddata");
        		layer.confirm('确认删除该配置吗？',{
        			btn: ['确定','取消']
        		}, function(){
        			delectconfig(type,cid,id);
        		}, function(){
        			layer.msg("取消删除",{icon: 1,time:700});
        		});
        	}
        	
        	//删除的公共方法
        	function delectconfig(type,cid,pid) {
        		$.ajax({
        			type : "post",
        			data : {type:type,cid:cid,id:id},
        			url : "/systerm/deleteconfig",
        			dataType : "json",
        			success : function(data){
        				layer.msg("删除成功",{icon: 1,time:500},function(){ window.location.reload();});
        			}
        		});
    		}
        });
        
        
        
	});
	
	//配置项
	$(function(){
		$("#validatebound-form1").validate({
			rules: { },
			messages: { },
			submitHandler: function(form) {
				var name = $("#configname").val();
				var code = $("#code").val();
				var type = $("#ctype").val();
				var id = $("#conid").val();
				$.ajax({
					type : "post",
					url : "/systerm/addconfigure",
					data : {name : name , code : code , type : type , id : id},
					dataType : "json",
					success : function(data){
						layer.msg("添加成功!", {icon: 1,time:500},function(){ window.location.reload();});
					}					
				});
	  		}
		});
		//配置项详情
		$("#validatebound-form2").validate({
			rules: { },
			messages: { },
			submitHandler: function(form) {
				var name = $("#configitemname").val();
				var value = $("#value").val();
				var ordernumber = $("#ordernumber").val();
				var cid = $("#cid").val();
				var pid = $("#pid").val();
				var type = $("#citype").val();
				var id = $("#coniid").val();
				$.ajax({
					type : "post",
					url : "/systerm/addconfigureitem",
					data : {name:name,value:value,ordernumber:ordernumber
						,cid:cid,pid:pid,type:type,id:id},
					dataType : "json",
					success : function(data){
						layer.msg("添加成功!", {icon: 1,time:500},function(){ window.location.reload();});
					}					
				});
	  		}
		});
	});

</script>

</body>
</html>